package com.example.supermarket.service;

import com.example.supermarket.entity.Goods;
import com.example.supermarket.mapper.GoodsMapper;
import com.example.supermarket.utils.GiteeImgBedUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Bosen
 * @date 2021/10/12 19:30
 */
@Service
public class AdminGoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * <p>获取所有商品信息</p>
     */
    public List<Goods> findAll() {
        List<Goods> list = goodsMapper.findAll();
        for (Goods goods : list) {
            goods.setImage(GiteeImgBedUtils.PRE + goods.getImage());
        }
        return list;
    }

    /**
     * <p>添加商品</p>
     * @return
     *-1 图片上传失败
     * 0 sql语句执行失败
     * 1 添加商品成功
     * 2 必填信息不能为空
     */
    public int addGoods(String name, String describe, BigDecimal price, MultipartFile img) {
        if ("".equals(name) || "".equals(describe)){
            return 2;
        }
        if (price == null){
            return 2;
        }
        int goodsMapperResult = 0;

        if ("".equals(img.getOriginalFilename())){
            return -1;
        }else{
            // 接收文件工具类返回的文件位置
            String imgUrl = GiteeImgBedUtils.upload("data/goods/", img);
            // 文件上传失败
            if (imgUrl == null){
                return -1;
            }
            // 执行插入
            goodsMapperResult = goodsMapper.insertGoods(name, imgUrl, describe, price);
            if (goodsMapperResult != 1){
                // sql语句执行失败，将已上传的图片移除
                GiteeImgBedUtils.delete(imgUrl);
                return 0;
            }
        }
        return 1;
    }

    /**
     * <p>通过id值删除商品逻辑处理</p>
     * @return
     *-1 删除失败
     * 1 删除成功
     */
    public int delGoods(int id){
        // 获取goods示例图片的地址
        String imgUrl = goodsMapper.findById(id).getImage();
        // 删除数据库中goods的数据
        int sql = goodsMapper.deleteById(id);
        if (sql!=1){
            return -1;
        }else{
            if (GiteeImgBedUtils.delete(imgUrl)) {
                return 1;
            }
            return -1;
        }
    }

    /**
     * <p>修改商品信息</p>
     * @return
     *-1 图片上传失败
     * 0 sql语句执行失败
     * 1 修改商品成功
     * 2 必填信息不能为空
     */
    public int updateGoods(int id, String name, String describe, BigDecimal price,MultipartFile img) {
        if ("".equals(name) || "".equals(describe)){
            return 2;
        }
        if (price == null){
            return 2;
        }
        int goodsMapperResult = 0;

        if ("".equals(img.getOriginalFilename())){
            // 执行插入
            goodsMapperResult = goodsMapper.updateGoods1(id, name, describe, price);
            if(goodsMapperResult != 1){
                return 0;
            }
        }else{
            String imgUrl = GiteeImgBedUtils.upload("data/goods/", img);
            // 文件上传失败
            if (imgUrl == null){
                return -1;
            }
            // 执行插入
            Goods tempGoods = goodsMapper.findById(id);
            goodsMapperResult = goodsMapper.updateGoods2(id, name, imgUrl, describe, price);
            if (goodsMapperResult != 1){
                // sql语句执行失败，将已上传的图片移除
                GiteeImgBedUtils.delete(imgUrl);
                return 0;
            }else{
                // 新图片已插入，将旧图删除
                GiteeImgBedUtils.delete(tempGoods.getImage());
            }
        }
        return 1;
    }
}
